package com.ruoyi.mes.controller;


import com.alibaba.fastjson.JSONObject;
import com.ruoyi.mes.domain.DeviceCommand;
import com.ruoyi.mes.domain.DeviceStatus;
import com.ruoyi.mes.utils.MessageCallback;
import com.ruoyi.mes.utils.MqttClientUtil;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@RestController
@RequestMapping("/MqttClient")
public class MqttClientController {



    @Autowired
    private MqttClientUtil mqttClientUtil;


    @PostMapping("/Publish")
    public void Publish(@RequestParam String device, @RequestParam String command) throws MqttException {
        String topic="chanxian/ljt";
        try {
            //           DeviceCommand deviceCommand = new DeviceCommand(deviceId, command);
//            mqttClientUtil.publish(topic, deviceCommand);
            JSONObject json = new JSONObject();
            json.put("device", device);
            json.put("command", command);

            mqttClientUtil.publish(topic, json.toJSONString());

            // 注册回调以处理响应
//            mqttClientUtil.registerCallback(device, status -> {
//                // 这里可以添加业务逻辑，如日志记录
//                System.out.println("设备 " + device + " 状态更新: " + status.getStatus());
//                return status;
//            });

        } catch (Exception e) {
            e.printStackTrace();

        }
    }

    @PostMapping("/Subscribe")
    public void Subscribe(String deviceId) throws MqttException {
        String topic="chanxian/ljt";
        try {
            mqttClientUtil.subscribe(topic);

        } catch (MqttException e) {
            throw new RuntimeException(e);
        }
    }

    //获取单个设备
    @GetMapping("/status")
    public DeviceStatus getDeviceStatus(@PathVariable String device) {

        return mqttClientUtil.getDeviceStatus(device);
    }

//    @GetMapping("/status/all")
//    public Map<String, DeviceStatus> getAllDeviceStatuses() {
//        return mqttClientUtil.getAllDeviceStatuses();
//    }














//    @Autowired
//    private MqttClientUtil mqttClientUtil;
//
//    private final Map<String, DeviceStatus> deviceStatusMap = new ConcurrentHashMap<>();
//
//    //发布主题信息
//    @PostMapping("/Publish")
//    public void Publish(@RequestParam String topic, @RequestParam String message) throws MqttException {
//        try {
//            mqttClientUtil.publish(topic, message);
//
//
//        } catch (Exception e) {
//            e.printStackTrace();
//
//        }
//
//    }
//    //订阅主题，返回回调函数MessageCallback的MqttMessage的值
//    @PostMapping("/Subscribe")
//    public void Subscribe(@RequestParam String topic) throws MqttException {
//        try {
//                mqttClientUtil.subscribe(topic, (msg) -> {
//                       String payload = new String(msg.getPayload());
//                       System.out.println("回调处理: " + payload);
//                       // 这里可以添加业务逻辑，如保存到数据库
// 创建实体对象
//DeviceStatus status = new DeviceStatus();
//    status.setTopic(t);
//    status.setDeviceId(data.getString("devices"));
//    status.setStatus(data.getInteger("status"));
//    status.setRawData(payload);
//    status.setTimestamp(System.currentTimeMillis());
//
//    // 保存到数据库
//    deviceStatusRepository.save(status);
//
//    System.out.println("保存设备状态: " + status);
//
//
//        } catch (MqttException e) {
//            throw new RuntimeException(e);
//        }
//
//
//    }
//
//    //获取主题回调函数
//    @PostMapping("/TopicCallback")
//    public MessageCallback TopicCallback(@RequestParam String topic, @RequestParam String message) throws MqttException {
//       return mqttClientUtil.getTopicCallback(topic);
//    }
//
//    //获取设备状态
//    @GetMapping("/GetStatus/{topic}")
//    public JSONObject getDeviceStatus(@PathVariable String topic) {
//        return mqttClientUtil.getDeviceStatus(topic);
//    }
//
//    @GetMapping("/GetAllStatuses")
//    public Map<String, JSONObject> getAllDeviceStatuses() {
//        return mqttClientUtil.getAllDeviceStatuses();
//    }
//    @Autowired
//    private MqttClientUtil mqttClientUtil;
//
//    private final Map<String, DeviceStatus> deviceStatusMap = new ConcurrentHashMap<>();
//
//    @PostMapping("/Publish")
//    public void Publish(@RequestParam String topic, @RequestParam String deviceId, @RequestParam String command) throws MqttException {
//        try {
//            //           DeviceCommand deviceCommand = new DeviceCommand(deviceId, command);
////            mqttClientUtil.publish(topic, deviceCommand);
//            JSONObject json = new JSONObject();
//            json.put("device", deviceId);
//            json.put("command", command);
//
//            mqttClientUtil.publish(topic, json.toJSONString());
//
//        } catch (Exception e) {
//            e.printStackTrace();
//
//        }
//
//    }
//
//    @PostMapping("/Subscribe")
//    public void Subscribe(@RequestParam String topic) throws MqttException {
//        try {
//            mqttClientUtil.subscribe(topic);
//
//        } catch (MqttException e) {
//            throw new RuntimeException(e);
//        }
//
//
//    }
//    //获取单个设备
//    @GetMapping("/status")
//    public DeviceStatus getDeviceStatus(@PathVariable String deviceId) {
//
//        return mqttClientUtil.getDeviceStatus(deviceId);
//    }
//
//    @GetMapping("/status/all")
//    public Map<String, DeviceStatus> getAllDeviceStatuses() {
//        return mqttClientUtil.getAllDeviceStatuses();
//    }

}


